<?php
// +----------------------------------------------------------------------
// | wttai [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wttai.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 老司机 <wutongtai@wttai.com> <http://www.wttai.com>
// +----------------------------------------------------------------------

namespace Common\Model;
use Think\Model;

/**
 * 广告模型
 */
class AdvertiseContentModel extends BaseModel{


    /**
     * 获取不同位置的广告
     * @param int $position 广告的位置   1:Slide幻灯片2:Home_left首页左3:Home_right首页右4:Home_bottom首页中间5:Home_bottom首页底部6:Details_top详情顶部
     * @param int $type 类型 1分类 ，2 标签
     * @return array $result 标签或标签数组
     * 
     */
    public function get_position_ad($position)
    {
        //排序
        $order='sort desc ,start_time desc ';
        $map['effect']=1;
        $adInfo=$this->where(array('category_id'=>$position))->order($order)->where($map)->select();
        $ad=array();
        foreach($adInfo as $k=>$v){
            $i=0;
            if($v['ad_type']==1){
                
                //查询当前的配置信息
                $data=M('AdValue')->where(array('ad_id'=>$v['id']))->select(array('index'=>'ad_conf_name'));
                
                foreach ($data as $key=>$value){
                    $info[$key]=$value['ad_conf_value'];
                }
                
                $data=$this->getUrlConetent($v['ad_type'],$info);
              //dump($data);
                foreach ($data['data'] as $keys=>$val){
                    
                    $adInfo[$k]['data'][$keys]['title']=$val['title'];
                    $adInfo[$k]['data'][$keys]['url']=$val['url'];
                    $adInfo[$k]['data'][$keys]['img']=$val['img'];
                }
               
                
            }elseif( $v['ad_type']==2){
                //查询当前的配置信息
                $data=M('AdValue')->where(array('ad_id'=>$v['id']))->select(array('index'=>'ad_conf_name'));
                
                foreach ($data as $key=>$val){
                    $info[$key]=$val['ad_conf_value'];
                }
                
                $data=$this->getUrlConetent($v['ad_type'],$info);
                foreach ($data['datas'] as $keys=>$val){
                    $adInfo[$k]['data'][$keys]['title']=($info['scene']==1 ?$val['goods_name'] : $val['store_name']); 
                    $adInfo[$k]['data'][$keys]['img']=$val['goods_image'];
                    $adInfo[$k]['data'][$keys]['url']=$val['url'];
                }
               
            }elseif($v['ad_type']==3){
                
            }
            $ad=array_merge($ad,$adInfo[$k]['data']);
        }  
       
        return $ad;
        
    }
    /*  
     *根据接口获取广告信息
     *
     */
  public function getUrlConetent($ad_type,$data){
       $data['count']=($data['count'] ?:1);
        if($ad_type==1){
            
            //如果是测试环境
            //$url="http://sns.dev.wttai.com".'api16/snsActivityList';
            $url=C('SNS_AD_URL').'/index.php?s=/api/api16/snsActivityList';
            //返回过来的数据存储到数据库中
              
        }elseif($ad_type==2){
            $url= C('D2B_URL').'api/index.php?c=goods&a=list4m&class=1';
            
        }
        $res=getApiDataByUrl($url,$data);
        return $res;
    }
    
}
